//
//  DKRetWeetView.swift
//  Sina
//
//  Created by 赵柏清 on 16/8/4.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SnapKit

class DKRetWeetView: UIView {
    /// 全局约束
    private var RetConstraints : Constraint?
    /// 定义模型来接收数据
    var retModel : DKHomeCellViewModel? {
        didSet {
            contentLabel.text = retModel?.statusModel?.retweeted_status?.text
            
            RetConstraints?.uninstall()
            
            if let picArray = retModel?.statusModel?.retweeted_status?.pic_urls where picArray.count > 0 {
                
                photoView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.RetConstraints = make.bottom.equalTo(photoView).constraint
                    
                })
                
                photoView.pic_urls = picArray
                
            } else {
                
                photoView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.RetConstraints = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.yellowColor()
        addSubview(contentLabel)
        addSubview(photoView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
        }
        
        photoView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.left.equalTo(self).offset(HomeCellMargin)
            
            make.width.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            // 和原创微博一样,先对self进行约束,并记录其约束(没有图片的时候要进行修改)
            self.RetConstraints = make.bottom.equalTo(photoView.snp_bottom).constraint
        }
    }

    
    // MARK : 懒加载
    private lazy var contentLabel : UILabel = {
        
        let label = UILabel()
        label.text = "转发微博"
        label.numberOfLines = 0
        
        return label
    }()
    /// 转发微博的图片
    private lazy var photoView : DKPhotoView = DKPhotoView()
}
